#服务器地址
#serverUrlPre = 'http://visitveb.duapp.com/index.php?s='
#是否已经开始踩流量
visiting = false
#待检测的ip
lists = []
#测试代理IP等级的网址
rankUrl = 'http://visitweb.sinaapp.com/index.php?'
#获取待检测代理IP的地址
getListsUrl = "#{serverUrlPre}Usercheck/lists"
#报告检测结果的地址
reportUrl = "#{serverUrlPre}Usercheck/report"
sellername = ''
dataKey='thispagekey'

@startCheck = ->
	visiting = false
	#sellername = $('#sellername').val()
	#如果有就直接开始
	if lists?.length
		checkOne()
	else
		$.getJSON getListsUrl,{sellername:sellername},(data)->
			if !data
				window.startCheck=$.noop
				return off
			if visiting
				return false
			lists = data
			checkOne()
@stopCheck = ->
	visiting = true

#开始检测
checkOne = ->
	if !lists or !lists.length or visiting
		return false
	#随机选一个验证
	lists.sort((a,b)->
		Math.random() - 0.5
	)
	data = lists.shift()

	config =
		mode:'fixed_servers'
		rules:
			proxyForHttp:
				scheme:data.agt_type.toLowerCase()
				host:data.agt_ip
				port:parseInt data.agt_port
			bypassList:[
				'img*.taobaocdn.com','pics.taobaocdn.com'
				'g.tbcdn.cn','a.tbcdn.cn','l.tbcdn.cn'
				'img.taobao.com'
				'i.mmcdn.cn'
				'g.searc*.alicdn.com','gi*.md.alicdn.com'
				#aliyuncdn
				'uaction.aliyuncdn.com'
				#各省cdn
				'bj*.alicdn.com'
				'sh*.alicdn.com'
				'gd*.alicdn.com'
				'zj*.alicdn.com'
				'js*.alicdn.com'
				'fj*.alicdn.com'
				#自己
				'127.0.0.1','localhost'
				'visitveb.duapp.com'
				'visitweb.allalla.com'
				#server
				'developer.baidu.com','sma.duapp.com','*.bdimg.com'
			]
	setting =
		value:config
		scope:'regular'
		check:true
	chrome.proxy.settings.set(setting,->
		if visiting
			return off
		reportPre = "#{reportUrl}/agt_id/#{data.agt_id}/sellername/#{sellername}"
		handler=
			dataType:'json'
			timeout:16000
			complete:startCheck
			error:->
				if visiting
					return false
				$.getJSON reportPre,crt_rank:-1,sellername:sellername
				#不写有问题
				true
			success:(rank)->
				if visiting
					return false
				#设置等级
				$.getJSON reportPre,crt_rank:rank,sellername:sellername
		$.ajax("#{rankUrl}#{new Date().getTime()}",handler)
	)

#得到用户
unless '#start' == location.hash
	#重写代理设置
	proxySet = chrome.proxy.settings.set
	chrome.proxy.settings.set = (setting,callback)->
		if setting.check
			delete setting.check
		else
			callback = ->
				visiting = true
				callback() if callback
		#proxySet setting,callback
		proxySet.call chrome.proxy.settings,setting,callback
	chrome.storage.local.get dataKey,(item)->
		if item?[dataKey]?.sellername
			sellername = item[dataKey].sellername
			startCheck()

#设置ip地址的地区
$.getJSON "#{serverUrlPre}Cron/task", (data)->
	locationTimer = setInterval ->
		has = off
		for url,times of data
			unless times
				delete data[url]
				continue
			has = on
			data[url]--
			$.getJSON "#{serverUrlPre}#{url}",(data)->
				if 0==data
					delete data[url]
			break
		unless has
			clearInterval locationTimer
	,60*1000
